
angular.module('aaser.models').service('UserSession', function ($cookieStore, $http) {
    this.user = null;
    this.create = function (authUser) {
        this.user = {};
        this.user.name = authUser.user.name;
        this.user.type = authUser.user.type;
        this.user.token = authUser.tokenInfo;
        $cookieStore.put('user', this.user);
        this.setHeaders();
    };

    this.getFromCookie = function () {
        if (angular.isDefined($cookieStore.get('user'))) {
            this.user = $cookieStore.get('user');
            this.setHeaders();
            return true;
        }else{
            return false;
        }
    };

    this.setHeaders = function () {
        $http.defaults.headers.common['Authorization'] = 'Bearer ' + this.user.token.accessToken;

    };


    this.destroy = function () {
        this.user = null;
        $cookieStore.remove('user');
    };
    return this;
});